(function() {
  var checking_freeze, update_status, update_trctl_values;

  checking_freeze = function(form) {
    var in_checking;
    in_checking = form.form('get value', 'in_checking');
    if (in_checking !== 'true') {
      return;
    }
    $('body').css('background-color', 'black');
    return freeze_form(form);
  };

  update_status = function(form, record) {
    var $mapto_merid, $settl_merid, $trctl, $unit, chnl, clear, inside, _i, _len, _ref;
    inside = form.find('.dropdown.inside').dropdown('get value');
    chnl = form.find('.dropdown.chnl').dropdown('get value');
    if (!inside || !chnl) {
      return;
    }
    $settl_merid = form.find('.dropdown.settl_merid').addClass('disabled').dropdown('clear');
    $mapto_merid = form.find('.dropdown.mapto_merid').addClass('disabled').dropdown('clear');
    $settl_merid = form.find('.dropdown.settl_merid').addClass('disabled').dropdown('clear');
    $unit = form.find('.dividing.unit, .fields.unit').hide();
    $trctl = form.find('.dividing.trctl, .fields.trctl').hide();
    if (parseInt(chnl) === 0) {
      _ref = [$unit, $trctl];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        clear = _ref[_i];
        clear.find('.dropdown').dropdown('clear');
        clear.find('.checkbox').checkbox('uncheck');
        clear.find('input').val('');
      }
      return;
    }
    $settl_merid.removeClass('disabled');
    if (inside === 'true') {
      $mapto_merid.addClass('loading');
      $.post('/merch/ajax/mapto_merids', {
        chnl: chnl
      }, function(resp) {
        var color, item, items, _j, _len1, _ref1;
        $mapto_merid.removeClass('loading');
        if (!resp.succ) {
          return;
        }
        items = '<div class="item" data-value=" ">无</div>';
        _ref1 = resp.records;
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
          item = _ref1[_j];
          color = item.shaded ? 'orange' : '#888';
          items += "<div class='item' data-value='" + item.uuid + "'>";
          items += "<div class='right floated' style='font-size:small; color:" + color + ";'>" + item.merno + "</div>";
          items += "" + (item.name.substring(0, 25));
          items += "</div>";
        }
        $mapto_merid.find('.menu').html(items);
        $mapto_merid.removeClass('disabled').dropdown('refresh');
        if (record && record.mapto_merid) {
          $mapto_merid.dropdown('set selected', record.mapto_merid);
        }
        return checking_freeze(form);
      });
    }
    $unit.show();
    return $trctl.show();
  };

  update_trctl_values = function(form, trctl) {
    var field, trcode, _i, _len, _ref, _results;
    form.find('.checkbox.trcode').checkbox('uncheck');
    $.each(trctl.trcodes, function(k, v) {
      return form.find(".checkbox.trcode." + k).checkbox('check');
    });
    _ref = ['t20', 't60', 'tz0'];
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      trcode = _ref[_i];
      if (!trctl[trcode]) {
        continue;
      }
      _results.push((function() {
        var _j, _len1, _ref1, _results1;
        _ref1 = window.trctl_fields;
        _results1 = [];
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
          field = _ref1[_j];
          _results1.push(form.find(".fields.trctl." + trcode + " input[name=" + field + "]").val(trctl[trcode][field] || ''));
        }
        return _results1;
      })());
    }
    return _results;
  };

  $(function() {
    var form, record;
    form = $('form.main');
    form.form({
      inline: true,
      fields: {
        inside: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        chnl: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        merno: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }, {
              type: 'doesntContain[ ]'
            }
          ]
        },
        t20_fee: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        name: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        contact: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        address: {
          optional: false,
          rules: [
            {
              type: 'empty'
            }
          ]
        },
        email: {
          optional: true,
          rules: [
            {
              type: 'email'
            }
          ]
        },
        telephone: {
          optional: true,
          rules: [
            {
              type: 'number'
            }
          ]
        },
        mobile: {
          optional: true,
          rules: [
            {
              type: 'number'
            }
          ]
        },
        fax: {
          optional: true,
          rules: [
            {
              type: 'number'
            }
          ]
        },
        unit_assets: {
          optional: true,
          rules: [
            {
              type: 'number'
            }
          ]
        }
      }
    });
    checking_freeze(form);
    record = form.form('get value', 'record');
    if (record && record.trim().length > 0) {
      form.form('set value', 'record', '');
      record = JSON.parse(record);
      record.chnl = '' + record.chnl;
    } else {
      record = null;
    }
    form.find('.dropdown.inside').dropdown({
      onChange: function(value, text, $choice) {
        return update_status(form, record);
      }
    });
    form.find('.dropdown.chnl').dropdown({
      onChange: function(value, text, $choice) {
        if (parseInt(value) === 0) {
          form.find('.dropdown.inside').dropdown('set selected', 'true').addClass('disabled');
        } else {
          form.find('.dropdown.inside').removeClass('disabled');
        }
        return update_status(form, record);
      }
    });
    form.find('.ui.button#trcode_all').click(function() {
      return form.find('.checkbox.trcode').each(function() {
        return $(this).checkbox('check');
      });
    });
    form.find('.ui.button#trcode_none').click(function() {
      return form.find('.checkbox.trcode').each(function() {
        return $(this).checkbox('uncheck');
      });
    });
    form.find('.dropdown.trctl_t').dropdown({
      onChange: function(value, text, $choice) {
        var $this;
        $this = $(this).addClass('loading');
        return $.post('/merch/ajax/trctl_info', {
          trctl_uuid: value
        }, function(resp) {
          $this.removeClass('loading');
          if (!resp.succ) {
            return;
          }
          if (resp.record.trctl) {
            return update_trctl_values(form, resp.record.trctl);
          }
        });
      }
    });
    form.find('button#input_delete').click(function(evt) {
      if ($(this).hasClass('has_clicked')) {
        $(this).removeClass('has_clicked').text('删 除');
        $(this).addClass('loading');
        return $.post('/merch/input/delete', {
          serial: form.form('get value', 'serial'),
          uuid: form.form('get value', 'uuid')
        }, function(resp) {
          return form.find('button#input_delete').removeClass('loading');
        });
      } else {
        return $(this).addClass('has_clicked').text('确认删除');
      }
    });
    form.find('button#pass').click(function() {
      var effect_time;
      if ($(this).hasClass('has_clicked')) {
        effect_time = form.form('get value', 'effect_time');
        if (!effect_time || effect_time.trim().length === 0) {
          alert('请选择信息生效的时间');
          return;
        }
        $(this).removeClass('has_clicked').text('通 过');
        form.attr('action', '/merch/check/pass');
        return form.submit();
      } else {
        form.find('button#reject').text('拒 绝').removeClass('has_clicked');
        form.find('.fields#reject_reason').hide();
        form.find('.fields#effect_time').show();
        return $(this).addClass('has_clicked').text('确认通过');
      }
    });
    form.find('button#reject').click(function() {
      var reason;
      if ($(this).hasClass('has_clicked')) {
        reason = form.form('get value', 'reject_reason');
        if (!reason || reason.trim().length === 0) {
          alert('请输入拒绝的原因，以便录入人员可以有针对性的进行修改');
          return;
        }
        $(this).removeClass('has_clicked').text('拒 绝');
        form.attr('action', '/merch/check/reject');
        return form.submit();
      } else {
        form.find('button#pass').text('通 过').removeClass('has_clicked');
        form.find('.fields#effect_time').hide();
        form.find('.fields#reject_reason').show();
        return $(this).addClass('has_clicked').text('确认拒绝');
      }
    });
    if (record) {
      form.form('set values', record);
      if (record.trctl) {
        update_trctl_values(form, record.trctl);
      }
    }
    return form.submit(function(evt) {
      var field, trcode, trctl, val, _i, _j, _len, _len1, _ref, _ref1;
      if (!$(this).form('is valid')) {
        return evt.preventDefault();
      }
      trctl = {};
      trctl.trcodes = {};
      form.find('.checkbox.trcode').each(function() {
        if ($(this).checkbox('is checked')) {
          return trctl.trcodes[$(this).find('input').attr('name')] = true;
        }
      });
      _ref = ['t20', 't60', 'tz0'];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        trcode = _ref[_i];
        trctl[trcode] = {};
        _ref1 = window.trctl_fields;
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
          field = _ref1[_j];
          val = form.find(".fields.trctl." + trcode + " input[name=" + field + "]").val();
          if (val && val.trim().length > 0) {
            trctl[trcode][field] = val;
          }
        }
      }
      return form.form('set value', 'trctl', JSON.stringify(trctl));
    });
  });

}).call(this);
